const { startProcess, qiniuUpload }  = require('../libs/utils'),
      { addSliderData } = require('../services/Slider'),
      { addPosterBook } = require('../services/PosterBook'),
      { addRecomBook } = require('../services/RecomBook'),
      { addAllBookData } = require('../services/AllBook'),
      { addAuthorData } = require('../services/Author'),
      { qiniu } = require('../config/config'),
      { CRAWLER } = require('../config/error_config'),
      { returnInfo } = require('../libs/utils');

class Crawler {

    //爬取百度阅读首页轮播图
    async crawlSliderData(ctx, next) {
        const retData = await new Promise((resolve) => {
            startProcess({
                path: '../crawlers/slider',
                async message (data) {//进到message这里证明数据爬取成功了，上传七牛，上传数据库都在这里面进行
                    // console.log(data);
                    
                    data.map(async item => {
                        if (item.imgUrl && !item.imgKey) {//因为我们要给图片另起名称，所以这个imgKey要为空才行
                            try {//这是网络传输，有可能会报错，所以要try cacth 一下
                                 //上传成功了内部返回的是一个{ key }给这个imgData
                                const imgData = await qiniuUpload({//上传七牛
                                    url: item.imgUrl,
                                    bucket: qiniu.bucket.gc_project.bucket_name,
                                    ext: '.jpg'
                                });
    
                                if (imgData.key) {
                                    item.imgKey = imgData.key;
                                }
    
                                // console.log(item);
                                
                                const result = await addSliderData(item);
    
                                if (result) {
                                    console.log('Data create OK');
                                } else {
                                    console.log('Data create failed');
                                }
                            } catch (error) { 
                                // throw new Error(error);
                                console.log(error);
                            }
                        }
                    });
    
                    resolve(returnInfo(CRAWLER.CRAWL_SUCCESS));
                },
                async exit (code) {
                    console.log(code);
                },
                async error (error) {
                    console.log(error);
                    resolve(returnInfo(CRAWLER.CRAWL_FAILED));
                }
            })
        })
        
        ctx.body = await retData;
    }

    //爬取百度阅读首页轮播图下面的三张广告图，
    async crawlPosterBook(ctx, next) {
        const retData = await new Promise(resolve => {
            startProcess({
                path: '../crawlers/posterBook',
                async message (data) {
                    console.log(data);
                    data.map(async item => {
                        try {
                            if (item.imgUrl && !item.imgKey) {
                                const posterData = await qiniuUpload({
                                    url: item.imgUrl,
                                    bucket: qiniu.bucket.gc_project.bucket_name,
                                    ext: '.jpg'
                                })

                                if (posterData.key) {
                                    item.imgKey = posterData.key;
                                }
                            }
                            console.log(item);

                            const result = await addPosterBook(item);

                            if (result) {
                                console.log('Data create OK');
                            } else {
                                console.log('Data create failed');
                            }
                        } catch (error) {
                            // throw new Error(error);
                            console.log(error);
                        }
                    })

                    resolve(returnInfo(CRAWLER.CRAWL_SUCCESS));
                },
                async exit (code) {
                    console.log(code);
                },
                async error (error) {
                    console.log(error);
                    resolve(returnInfo(CRAWLER.CRAWL_FAILED));
                }
            })
        });

        ctx.body = await retData;
    }

    //爬取'推荐书本'
    async crawlRecomBook (ctx, next) {
       const retData = await new Promise(resolve => {
        startProcess({
            path: '../crawlers/recomBook',
            async message (data) {
                // console.log(data);
                data.map(async item => {
                    try {//这是网络传输，有可能会报错，所以要try cacth 一下
                        if (item.href && !item.imgKey) {//因为我们要给图片另起名称，所以这个imgKey要为空才行
                            //上传成功了内部返回的是一个{ key }给这个bookData
                            const bookData = await qiniuUpload({//上传七牛
                                url: item.href,
                                bucket: qiniu.bucket.gc_project.bucket_name,
                                ext: '.jpg'
                            });

                            if (bookData.key) {
                                item.imgKey = bookData.key;
                            }
                        }

                        // console.log(data);
                        const result = await addRecomBook(item);
                        if (result) {
                            console.log('Data create OK!')
                        } else {
                            console.log('Data create failed!')
                        }

                    } catch (error) { 
                        // throw new Error(error);
                        console.log(error);
                    }
                });

                resolve(returnInfo(CRAWLER.CRAWL_SUCCESS));
            },
            async exit (code) {
                console.log(code);
            },
            async error (error) {
                console.log(error);

                resolve(returnInfo(CRAWLER.CRAWL_FAILED));
            }

        })
       })

       ctx.body = await retData;
    }

    //全部书本
    async crawlAllBooksData (ctx, next) {
       const retData = await new Promise( resolve => {
        startProcess({
            path: '../crawlers/allBooks',
            async message (data) {
                // console.log(data);
                data.map(async item => {
                    if (item.imgUrl && !item.imgKey) {
                        try {
                            //上传成功了内部返回的是一个{ key }
                            const bookData = await qiniuUpload({//上传七牛
                                url: item.imgUrl,
                                bucket: qiniu.bucket.gc_project.bucket_name,
                                ext: '.jpg'
                            });
        
                            if(bookData.key) {
                                item.imgKey = bookData.key
                            }
                            // console.log(item);
        
                            const result = await addAllBookData(item);
    
                            if (result) {
                                console.log('Data create OK!')
                            } else {
                                console.log('Data create failed');
                            }
                        } catch (error) {
                            // throw new Error(error);
                            console.log(error);
                        }
                    }

                })

                resolve(returnInfo(CRAWLER.CRAWL_SUCCESS));
            },
            async exit (code) {
                console.log(code);
            },
            async error (error) {
                console.log(error);

                resolve(returnInfo(CRAWLER.CRAWL_FAILED));
            }
        })
       })

       ctx.body = await retData;
    }

    //明星作者
    async crawlAuthor (ctx, next) {
        const retData = await new Promise( resolve => {
            startProcess({
                path: '../crawlers/author',
                async message (data) {
                 
                    data.map(async item => {
                        if (item.imgUrl && !item.imgKey) {
                            try {
                                //上传成功了内部返回的是一个{ key }
                                const imgData = await qiniuUpload({//上传七牛
                                    url: item.imgUrl,
                                    bucket: qiniu.bucket.gc_project.bucket_name,
                                    ext: '.jpg'
                                });
            
                                if(imgData.key) {
                                    item.imgKey = imgData.key
                                }
                                
                                console.log(item);

                                const result = await addAuthorData(item);
        
                                if (result) {
                                    console.log('Data create OK!')
                                } else {
                                    console.log('Data create failed');
                                }
                            } catch (error) {
                                // throw new Error(error);
                                console.log(error);
                            }
                        }
                    })

                    resolve(returnInfo(CRAWLER.CRAWL_SUCCESS));
                },
    
                async exit (code) {
                    console.log(code);
                },
    
                async error (error) {
                    console.log(error);

                    resolve(returnInfo(CRAWLER.CRAWL_FAILED));
                }
            })
        })

        ctx.body = await retData;
    }

}

module.exports = new Crawler();